// packageRecruit/pages/work-job-browse-detail/work-job-browse-detail.js
import { 
    workDetailInfo,
    getMonthGroupList,
    workExportNameList
} from '../../api/recruit'
import { getInsuranceProgramme } from '../../../api/app'
import user from '../../../utils/user'
import { toast,confirm,openDocument,dateFormat } from '../../../utils/util'
import global from '../../../utils/global'
const app = getApp()
Page({
    /**
     * 页面的初始数据
     */
    data: {
        jobId:'',
        list:[],
        jobDetailInfo:{},
        dictTypeSex:{},
        noSignInCount: 0,
        signInCount: 0,
        applyCount: 0,
        searchText:'',
        todoList:{},
        startTime:'',
        enTime:'',
        isPopupTop:false
    },
    static:{
        hasNextPage:false,
        pageSize:30,
        pageNum:1,
    },
    onLoad (options) {
        this.setData({
            jobId:options.id
        })
        // user.waitLogin().then(async ()=>{
        //     this.getWorkDetailInfo()
        //     const result = await getInsuranceProgramme({
        //         dictType:'saas_gender'
        //     })
        //     this.setData({
        //         dictTypeSex:result
        //     })
        // })
    },
    onReady () {

    },
    onShow () {
        this.setData({
            list:[],
            
        })
        this.static.pageNum = 1
        user.waitLogin().then(async ()=>{
            await this.getWorkDetailInfo()
            const result = await getInsuranceProgramme({
                dictType:'saas_gender'
            })
            this.setData({
                dictTypeSex:result
            })
            this.getMonthGroupList()
        })
    },
    // 吸顶事件监听
    onScrollSticky(event){
        const { date } = event.target.dataset
        if(event.detail.isFixed){
            this.data.list[date].isFixed = true 
            this.setData({
                list:this.data.list
            })
        }else{
            this.data.list[date].isFixed = false
            this.setData({
                list:this.data.list
            })
        }
    },
    async getWorkDetailInfo(){
        const result =  await workDetailInfo({
            id:this.data.jobId
        })
        result.jobTag = result.jobTag.split(';')
        if(result.jobTag.length === 1 && !result.jobTag[0]){
            result.jobTag = []
        }
        this.setData({
            jobDetailInfo:result
        })
    },
    async getMonthGroupList(){
        const result = await getMonthGroupList({
            pageSize: this.static.pageSize,
            pageNum: this.static.pageNum,
            searchText: this.data.searchText,
            jobId:this.data.jobId
        })
        for(let i in result.data){
            result.data[i].isFixed=false
        }
        const newObjKey = Object.keys(result.data)[0]
        const oldObjKey = Object.keys(this.data.list)[Object.keys(this.data.list).length-1]
        if(newObjKey === oldObjKey){
            this.data.list[oldObjKey].list.push(...result.data[newObjKey].list)
            delete result.data[newObjKey]
        }
        var obj = {...this.data.list,...result.data}
        this.setData({
            list:obj,
            hasNextPage:result.hasNextPage
        })
    },
    threshold(){
        if(!this.data.hasNextPage){
            return
        }
        this.static.pageNum = this.static.pageNum+=1
        this.getMonthGroupList()
    },
    // 搜索
    search(event){
        const { value } = event.detail
        this.static.pageNum = 1
        this.setData({
            searchText:value,
            list:[],
        })
        this.getMonthGroupList()
    },
    // 去岗位详情
    goWorkDetail(){
        wx.navigateTo({
            url: '/packageRecruit/pages/work-job-detail/work-job-detail?jobid=' + this.data.jobId,
        })
    },
    // 跳转到分享
    goPosterJob() {
        wx.navigateTo({
            url: '/packageRecruit/pages/poster-job/poster-job?id=' + this.data.jobDetailInfo.id,
        })
    },
    // 打电话
    onMakePhone(event){
        const { phone } = event.currentTarget.dataset
        wx.makePhoneCall({
            phoneNumber:phone
        })
    },
    onIsPopupTop(){
        this.setData({
            isPopupTop:!this.data.isPopupTop
        })
    },
    // jobId: this.data.jobId,
    // beginTime
    // endTime
    async exportWorkerList(){
        console.log('导出名单')
        if(!this.data.startTime){
            toast('请选择开始时间')
            return
        }
        if(!this.data.endTime){
            toast('请选择结束时间')``
            return
        }
        workExportNameList({
            jobId:this.data.jobId,
            beginTime:this.data.startTime,
            endTime:this.data.endTime
        }).then(res=>{
            // indexOf('https') !== -1 ? '/newjob' : ''
            const url = app.globalData.baseUrl +  global.zpUrl + '/common/download?fileName=' + res + '&delete=false'
            console.log(url)
            if (wx.canIUse('openDocument')) {
                const projectName = this.data.jobDetailInfo.jobName.replace(/\//g, '.') + this.data.jobId +'.xlsx'
                openDocument(url, projectName).then(() => {}, () => {
                    this.setData({
                        copyLinkStatus: true,
                        copyLinkContent: url
                    })
                })
            } else {
                this.setData({
                    copyLinkStatus: true,
                    copyLinkContent: url
                })
            }
        })
    },
    startTime(event){
        this.setData({
            startTime:event.detail.value
        })
        console.log(event, '开始时间')
    },
    endTime(event){
        this.setData({
            endTime:event.detail.value
        })
        console.log(event, '结束时间')
	},
	chooseDate() {
		
	}
})


